package com.youbug.kefu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.youbug.kefu.constant.ChatConstant;
import com.youbug.kefu.entity.Chat;
import com.youbug.kefu.service.IChatService;
import com.youbug.kefu.service.IStateService;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Service
public class StateServiceUseDB implements IStateService {

    @Autowired
    private IChatService chatService;

    @Override
    public Chat clientInChat(Long clientId) {

        QueryWrapper<Chat> wrapper = new QueryWrapper<>();

        wrapper.eq("client_id", clientId).in("state", Arrays.asList(ChatConstant.CHAT_STATE_TALKING, ChatConstant.CHAT_STATE_WAITTING))
                .gt("create_time", DateUtils.addMinutes(new Date(), -120));

        Chat chat = chatService.getOne(wrapper);

        return chat;
    }
}
